package com.example.tools.page;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@Data
@EqualsAndHashCode
public class PageQuery {
    /**
     * 默认分页索引
     */
    public static final int DEFAULT_INDEX = 1;
    /**
     * 默认分页大小
     */
    public static final int DEFAULT_SIZE = 20;

    @NotNull(message="index参数不能为空")
    @Min(value=0, message="index最小值为0")
    private Integer index;

    @NotNull(message="size参数不能为空")
    @Min(value=0, message="size最小值为0")
    private Integer size;

    /**
     * @Title: start
     * @Description: 分页起始值
     * @param @return    参数
     * @return Integer    返回类型
     * @throws
     */
    public Integer start() {
        index = index > 0 ? index : DEFAULT_INDEX;
        return (index - 1) * size;
    }

    /**
     * @Title: size
     * @Description: 分页大小
     * @param @return    参数
     * @return Integer    返回类型
     * @throws
     */
    public Integer size() {
        size = size > 0 ? size : DEFAULT_SIZE;
        return size;
    }
}
